package org.lanqiao.webfm.dao;

import java.io.Serializable;
import java.sql.SQLException;

import javacommon.base.BaseJdbcDao;
import javacommon.ijdbc.RowMapper;
import javacommon.util.StringUtils;
import javacommon.web.page.Page;
import javacommon.web.page.PageRequest;

import org.lanqiao.webfm.model.RightsRole;
import org.lanqiao.webfm.model.RightsUser;
import org.lanqiao.webfm.model.RightsUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RightsUserDao extends BaseJdbcDao<RightsUser> {
	private static final Logger LOGGER = LoggerFactory.getLogger(RightsUserDao.class);
	private RightsUserDao(){}
	private static RightsUserDao instance = new RightsUserDao();
	public static RightsUserDao getInstance(){
		return instance;
	}
	
	@Override
	public RightsUser getById(Serializable id) throws SQLException {
//		String sql = "select t1.*,t2.name as rolename from " + getTableName() + " t1 "
//				+ "left outer join cmn_rights_role t2 "
//				+ "on t1.role_id=t2.id "
//				+ "where t1.id=?";
		String sql1 = "select t1.* from " +getTableName()+" t1 where t1.id=?";
        RightsUser rightsUser = operation.queryForUniqueBean(sql1, RightsUser.class, id);
        rightsUser.setRightsRole(RightsRoleDao.getInstance().getById(rightsUser.getRoleId()));
        return rightsUser;
	}

	@Override
	public void save(RightsUser entity) throws SQLException {
		String sql = "insert into "
				+ getTableName()
				+ " (id,username,password,name,phonenumber,status,role_id,icon,birthday) values(dev_seq.nextval,?,?,?,?,?,?,?)";
		super.operation
				.execute(sql, entity.getUsername(), entity.getPassword(),
						entity.getName(), entity.getPhonenumber(),
						entity.getStatus(), entity.getRoleId(),entity.getIcon(),entity.getBirthday());
	}

	@Override
	public void update(RightsUser entity) throws SQLException {
		String sql = "update "+getTableName()
				+ " set username=?,password=?,name=?,phonenumber=?,status=?,"
				+ "role_id=?,icon=?,BIRTHDAY=?"
				+ " where id=?";
		super.operation
				.execute(sql, entity.getUsername(), entity.getPassword(),
						entity.getName(), entity.getPhonenumber(),
						entity.getStatus(), entity.getRoleId(),entity.getIcon(),entity.getBirthday(),entity.getId());
	}

	@Override
	public Page<RightsUser> findPage(PageRequest<RightsUser> pageRequest)
			throws SQLException {
		RightsUser user = (RightsUser) pageRequest;
		String sql = "select * from " + getTableName() + " where 1=1 ";

		if (StringUtils.hasLength(user.getUsername())) {
			sql += " and username like '%" + user.getUsername() + "%'";
		}
		if (StringUtils.hasLength(user.getName())) {
			sql += " and name like '%" + user.getName() + "%'";
		}
		if (StringUtils.hasLength(user.getPhonenumber())) {
			sql += " and phonenumber like '%" + user.getPhonenumber() + "%'";
		}
		if(StringUtils.hasLength(user.getStatus())) {
         	sql+=(" and  STATUS =  ")+(user.getStatus());
        }
		if (user.getRoleId() > 0) {
			sql += " and role_id =" + user.getRoleId();
		}
		if(StringUtils.hasLength(user.getBirthdayBegin())) {
            sql+=(" and  birthday >= ")+(user.getBirthdayBegin() );
        }
        if(StringUtils.hasLength(user.getBirthdayEnd())) {
            sql+=(" and  birthday <= ")+(user.getBirthdayEnd());
        }
		if (StringUtils.hasLength(user.getSortColumns())) {
			sql += " order by " + user.getSortColumns();
		}
		Page<RightsUser> page =  super.pageQuery(sql, pageRequest);
		for (RightsUser rightsUser : page.getResult()) {
			rightsUser.setRightsRole(RightsRoleDao.getInstance().getById(rightsUser.getRoleId()));
		}
		return page;
	}

	@Override
	public String getTableName() {
		return "cmn_rights_user";
	}

	@Override
	public String getIdColumnName() {
		return "id";
	}

//	@Override
//	public RowMapper<RightsUser> getMapper() {
//		return new RightsUserMapper();
//	}

	@Override
	public Class getEntityClass() {
		return RightsUser.class;
	}

	@Override
	public void deleteByIds(String[] ids) throws SQLException {
		String[] sqls = new String[ids.length];
		for (int i = 0; i < sqls.length; i++) {
			sqls[i] = "delete from cmn_rights_user where id=" + ids[i];
		}
		operation.executeBatch(sqls);
	}
}
